package com.ccx.coachsite.util;


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;



public class TelephoneUtil {
	/**
	 * 取得IMEI号
	 * @param ctx
	 * @return
	 */
	public static String getIMEI(Context ctx) {
		TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}
	
	public static String getIMSI(Context ctx) {
		TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = tm.getSubscriberId();
		if ("".equals(imsi))
			return null;
		return imsi;
	}

	/**
	 * 获取机器名称	如 milestone
	 * @return
	 */
	public static String getMachineName(){
		return android.os.Build.MODEL; 
	}
	/**
	 * 获取数字型API_LEVEL 如：4、6、7
	 * @return
	 */
	public static int getApiLevel(){
		int apiLevel = 7;
		try{
			apiLevel = Integer.parseInt(android.os.Build.VERSION.SDK);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return apiLevel;
		// android.os.Build.VERSION.SDK_INT  Since: API Level 4
		//return android.os.Build.VERSION.SDK_INT;
	}
	
	/**
	 * 获取字符串型的固件版本，如1.5、1.6、2.1
	 * @return
	 */
	public static String getFirmWareVersion(){
		final String version_3 =  "1.5";
		final String version_4 =  "1.6";
		final String version_5 =  "2.0";
		final String version_6 =  "2.0.1";
		final String version_7 =  "2.1";
		final String version_8 =  "2.2";
		final String version_9 =  "2.3";
		final String version_10 =  "2.3.3";
		final String version_11 =  "3.0";
		final String version_12 =  "3.1";
		final String version_13 =  "3.2";
		final String version_14 =  "4.0";
		final String version_15 = "4.0.3";
		String versionName = "";
		try{
			// android.os.Build.VERSION.SDK_INT  Since: API Level 4
			// int version = android.os.Build.VERSION.SDK_INT;
			int version = Integer.parseInt(android.os.Build.VERSION.SDK);
			switch(version){
			case 3:
				versionName = version_3;
				break;
			case 4:
				versionName = version_4;
				break;
			case 5:
				versionName = version_5;
				break;
			case 6:
				versionName = version_6;
				break;
			case 7:
				versionName = version_7;
				break;
			case 8:
				versionName = version_8;
				break;
			case 9:
				versionName = version_9;
				break;
			case 10:;
				versionName = version_10;
				break;
			case 11:
				versionName = version_11;
				break;
			case 12:
				versionName = version_12;
				break;
			case 13:
				versionName = version_13;
				break;
			case 14:
				versionName = version_14;
				break;
			case 15:
				versionName = version_15;
				break;				
			default:
				versionName = version_8;
			}
		}
		catch(Exception e){
			versionName = version_8;
		}
		return versionName;
	}

	/**
	 * 获取软件版本名称
	 * @return
	 */
	public static String getVersionName(Context ctx){
		String versionName = "";
		try {
			PackageInfo packageinfo = ctx.getPackageManager().getPackageInfo(
					ctx.getPackageName(), PackageManager.GET_INSTRUMENTATION);
			versionName = packageinfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionName;
	}
	/**
	 * 获取软件版本号 code
	 * @return
	 */
	public static int getVersionCode(Context ctx){
		int versionCode = 0;
		try {
			PackageInfo packageinfo = ctx.getPackageManager().getPackageInfo(
					ctx.getPackageName(), PackageManager.GET_INSTRUMENTATION);
			versionCode = packageinfo.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionCode;
	}
	/**
	 * 
	 * @Title: getPackageName 
	 * @Description: 获取包名
	 * @param ctx
	 * @return     
	 * @throws
	 */
	public static String getPackageName(Context ctx){
		String packageName = "";
		try {
			PackageInfo packageinfo = ctx.getPackageManager().getPackageInfo(
					ctx.getPackageName(), PackageManager.GET_INSTRUMENTATION);
			packageName = packageinfo.packageName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return packageName;
	}
	/**
	 * 返回屏幕分辨率,字符串型。如 320x480
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getScreenResolution(Context ctx) {
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager windowManager = (WindowManager) ctx
				.getApplicationContext().getSystemService(
						Context.WINDOW_SERVICE);
		windowManager.getDefaultDisplay().getMetrics(metrics);
		int width = metrics.widthPixels < metrics.heightPixels ? metrics.widthPixels : metrics.heightPixels;
		int height = metrics.widthPixels < metrics.heightPixels ? metrics.heightPixels : metrics.widthPixels;
		//String resolution = width + "x" + height;
		String resolution = width + "*" + height;
		return resolution;
	}
	
	/**
	 * 获取CPU的ABI
	 *
	 * @return
	 */
	public static String getCPUABI() {
		String abi = Build.CPU_ABI;
		abi = (abi == null || abi.trim().length() == 0) ? "" : abi;
		// 检视是否有第二类型，1.6没有这个字段
		try {
			String cpuAbi2 = Build.class.getField("CPU_ABI2").get(null).toString();
			cpuAbi2 = (cpuAbi2 == null || cpuAbi2.trim().length() == 0) ? null : cpuAbi2;
			if (cpuAbi2 != null) {
				abi = abi + "," + cpuAbi2;
			}
		} catch (Exception e) {
		}
		return abi;
	}
	/**
	 * 网络是否可用
	 * @param context
	 * @return
	 */
	public synchronized static boolean isNetworkAvailable(Context context) {
		boolean result = false;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = null;
		if (null != connectivityManager) {
			networkInfo = connectivityManager.getActiveNetworkInfo();
			if (null != networkInfo && networkInfo.isAvailable() && networkInfo.isConnected()) {
				result = true;
			}
		}
		return result;
	}
	
}
